//
//  ExtensionTableViewCell.m
//  UIHomeWork19_汽车之家找车页
//
//  Created by tusm on 16/8/10.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ExtensionTableViewCell.h"
#import "ExtensionCollectionViewCell.h"
#import "ExtensionList.h"
@interface ExtensionTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;

@end
@implementation ExtensionTableViewCell
//为array赋值
- (void)setExtensionArray:(NSMutableArray<ExtensionList *> *)extensionArray{

    _extensionArray = extensionArray;
}
- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
        flowLayOut.itemSize = CGSizeMake(90, 120);
        flowLayOut.minimumLineSpacing = 10;
        flowLayOut.minimumInteritemSpacing = 5;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:flowLayOut];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ExtensionCollectionViewCell class] forCellWithReuseIdentifier:@"poolExtension"];
        //颜色
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.alwaysBounceHorizontal = YES;
    }
    return _collectionView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleExtensionUrl];
    NSLog(@"%@", NSStringFromCGRect(self.contentView.bounds));
        [self.contentView addSubview:self.collectionView];
    }
    return self;
}

- (void)handleExtensionUrl{
    
    NSURL *allUrl = [NSURL URLWithString:@"http://adnewnc.app.autohome.com.cn/autov5.7.0/ad/infoad.ashx?version=6.2.0&platform=2&appid=2&networkid=0&adtype=1&provinceid=210000&cityid=0&lng=121.550814&lat=38.889714&gps_city=210200&pageid=8bfef6c6-c05a-4ef5-add9-8d39a74ee236&isretry=0&deviceid=357872061714383"];
    NSMutableURLRequest *allURLRequest = [NSMutableURLRequest requestWithURL:allUrl];
    allURLRequest.HTTPMethod = @"GET";
    NSURLSession *urlsession = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [urlsession dataTaskWithRequest:allURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSError *error = nil;
            NSDictionary *extensionDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSArray *listArray = extensionDic[@"result"][@"list"];
            self.extensionListArr = [NSMutableArray array];
            for (NSDictionary *dic in listArray) {
                ExtensionList *extensionList = [[ExtensionList alloc] initWithDictionary:dic];
                [_extensionListArr addObject:extensionList];
            }
            [_collectionView reloadData];
        });
    }];
    
    [dataTask resume];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _extensionArray.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ExtensionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolExtension" forIndexPath:indexPath];
    
    cell.extensionList = self.extensionArray[indexPath.row];
    return cell;

}
- (void)layoutSubviews{

    [super layoutSubviews];
    self.collectionView.frame = self.contentView.bounds;
}






- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
